package com.inetools.gwetons.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JavaScriptObject;

public class JsObjectDebugDump {
	private static JsObjectDebugDump _instance = null;
	
	private JsObjectDebugDump() {
	}
	
	public static synchronized JsObjectDebugDump getInstance() {
		if ( _instance == null) _instance = new JsObjectDebugDump();
		return _instance;
	}
	
	public static final void logToConsole( String logStr) {
		System.out.println( logStr);
	}
	public static final void logToGWTLog( String logStr) {
		GWT.log( logStr, null);
	}
	protected static int dumpSwitch = 0;
	public static void setDumpToConsole() {
		dumpSwitch = 0;
	}
	public static void setDumpToGwtLog() {
		dumpSwitch = 1;
	}
	
	public static final native void dumpJsObj( JavaScriptObject javaScriptObject, String displayName) /*-{
		for( var index in javaScriptObject) {
			var dumpSwitch = @com.inetools.gwetons.client.JsObjectDebugDump::dumpSwitch;
			if ( dumpSwitch < 1) {
				@com.inetools.gwetons.client.JsObjectDebugDump::logToConsole(Ljava/lang/String;)( 
					displayName + "[" + new String( index) + "] = " + new String( javaScriptObject[ index]));
			}
			else {
				@com.inetools.gwetons.client.JsObjectDebugDump::logToGWTLog(Ljava/lang/String;)( 
					displayName + "[" + new String( index) + "] = " + new String( javaScriptObject[ index]));
			}
		} // for
	}-*/;
	
	public static final native void dumpString( String message) /*-{
		var dumpSwitch = @com.inetools.gwetons.client.JsObjectDebugDump::dumpSwitch;
		if ( dumpSwitch < 1) {
			@com.inetools.gwetons.client.JsObjectDebugDump::logToConsole(Ljava/lang/String;)( message);
		}
		else {
			@com.inetools.gwetons.client.JsObjectDebugDump::logToGWTLog(Ljava/lang/String;)( message);
		}
	}-*/;
}
